/*
 * Copyright 2009 XueSong Guo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.webwheel.database.test.domain;

import cn.webwheel.database.annotations.Select;
import cn.webwheel.database.annotations.Update;

import java.util.List;

abstract public class User {

    private String id;
    private String nick;
    private Boolean gender;
    private List<Board> masterBoards;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public Boolean getGender() {
        return gender;
    }

    public void setGender(Boolean gender) {
        this.gender = gender;
    }

    public List<Board> getMasterBoards() {
        return masterBoards;
    }

    public void setMasterBoards(List<Board> masterBoards) {
        this.masterBoards = masterBoards;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (id != null ? !id.equals(user.id) : user.id != null) return false;

        return true;
    }

    public int hashCode() {
        return (id != null ? id.hashCode() : 0);
    }

    @Update("insert into user values(#this.id#, #this.nick#, #this.gender#)")
    abstract public void createUser() throws Exception;

    @Select("select * from user where id=#1#")
    abstract public void findById(String id) throws Exception;

    @Select("select * from user where id=#1#")
    abstract public User findUserById(String id) throws Exception;

    @Select("select * from user order by id")
    abstract public List<User> findUsers() throws Exception;

    @Select(value = "select * from user left outer join userboard on user.id=userboard.user left outer join board on userboard.board=board.id where user.id=#1#",
    result = "*masterBoards:board")
    abstract public User findUserWithMasterBoards(String id) throws Exception;
}
